'use strict';

const { Service } = require('egg');
const ModelName = 'PostJoin'
class RouterService extends Service {
    async getPage(where, page, pageSize) {
        return this.ctx.model[ModelName].findAndCountAll({
            where,
            offset: pageSize * (page - 1),
            limit: pageSize,
            order:[
                // id 降序
                ['create_time','DESC']
            ],
            include: [
                {
                    model: this.ctx.model.User,
                    as: 'user',
                },
                {
                    model: this.ctx.model.User,
                    as: 'company',
                },
                {
                    model: this.ctx.model.Resume,
                    as: 'resume',
                },
                {
                    model: this.ctx.model.Post,
                    as: 'post',
                }
            ]
        });
    }
    // 所有列表
    async allList(where) {
        return this.ctx.model[ModelName].findAll({
            where,
            order:[
                // id 降序
                ['create_time','DESC']
            ],
            include: [
                {
                    model: this.ctx.model.User,
                    as: 'user',
                },
                {
                    model: this.ctx.model.User,
                    as: 'company',
                },
                {
                    model: this.ctx.model.Resume,
                    as: 'resume',
                },
                {
                    model: this.ctx.model.Post,
                    as: 'post',
                }
            ]
        });
    }
    async findOne(id) {
        return this.ctx.model[ModelName].findOne({
            where: {
                id,
            },
        });
    }
    async create(data) {
        // post_id + user_id 唯一
        const postJoin = await this.ctx.model[ModelName].findOne({
            where: { post_id: data.post_id, user_id: data.user_id },
        })
        if (postJoin) {
            throw new Error('请勿重复投递')
            return
        }
        return this.ctx.model[ModelName].create(data);
    }
    async update(id, data) {
        let { userId } = data
        if (!id || !userId) return false
        const findOne = await this.findOne(id)
        // 判断is_read
        const is_read = findOne.is_read
        if (is_read > 0) {
            return true
        } else {
            // 插入一条消费记录,并且更新用户余额
            // 先查询配置，一条扣费多少钱
            const config = await this.ctx.model.RechargeConfig.findOne()
            if (!config) {
                return false
            } else {
                // 账户余额扣款
                const user = await this.ctx.model.User.findOne({
                    where: { id: userId }
                })
                if (user) {
                    let amount = config.amount || 0
                    let balance = user.balance || 0
                    balance = Number(balance) - Number(amount)
                    // 插入一条消费记录
                    await this.ctx.model.Recharge.create({
                        user_id: userId,
                        type: 0,
                        amount,
                        balance,
                        post_join_id: id,
                        desc: '投递简历扣款'
                    })

                    await this.ctx.model.User.update({
                        balance: parseFloat(balance).toFixed(2)
                    }, {
                        where: { id: userId }
                    })
                }
            }
        }
        return await this.ctx.model[ModelName].update({ is_read: 1 }, { where: { id } });
    }
    async deleteById(id) {
        return await this.ctx.model[ModelName].destroy({ where: { id } });
    }
}

module.exports = RouterService;
